//本脚本用到了同目录下的video-push目录和 njs_rov目录
//
const ps = require('child_process');

const nn = process.env._
//console.log(n);

//const p="/home/pabi"

function make_push_video_stream(){
	
	const child = ps.exec('ffmpeg -f video4linux2 -r 30 -s 1024x600 -i /dev/video0 -f mpegts -codec:v mpeg1video -an  -bf 0 -b:v 2200k -maxrate 3400k http://localhost:9999/push/test', (err,stdout,stderr)=>{
		if(!err){
			//console.log("ffmpeg 推视频流进程创建 OK！")
		}
	})
	
	child.on('exit',()=>{
		setTimeout(make_push_video_stream,500)
		//make_push_video_stream()
	})
}

function push_rtsp2mpeg_stream(){
	
	const child = ps.exec('ffmpeg  -i rtsp://192.168.1.160:8554/0 -f mpegts -codec:v mpeg1video -an -s 1024x600 -r 30 -bf 0 -b:v 2200k -maxrate 3400k http://localhost:9999/push/net_cam', (err,stdout,stderr)=>{
		if(!err){
			//console.log("ffmpeg 推视频流进程创建 OK！")
		}
	})
	
	child.on('exit',()=>{
		setTimeout(push_rtsp2mpeg_stream,500)
		//make_push_video_stream()
	})
}

function start_video_server(){
	
	const child = ps.exec(`${nn}  ${__dirname}/video-push/jsmpeg/server.js`, {cwd:`${__dirname}/video-push`},(err,stdout,stderr)=>{
		if(!err){
			//console.log("视频转发服务进程创建 OK！")
		}
	})
	
	child.on('exit',()=>{
		setTimeout(start_video_server,500)
	})
}


function start_uart_proxy(){
	const aa = new Date()
	const bb = aa.valueOf()
	const child = ps.exec(`${nn}  ${__dirname}/njs_rov/njs_serport.js >> ${__dirname}/njs_rov/log-njs-23509.log`,{cwd:`${__dirname}/njs_rov`},(err,stdout,stderr)=>{
		
	})
	
	child.on('exit',()=>{
		setTimeout(start_uart_proxy,500)
		//start_uart_proxy()
	})
}

start_uart_proxy();

start_video_server();

make_push_video_stream();

push_rtsp2mpeg_stream();

console.log("------------New Started!--------------",new Date())
console.log(`VAR nn=${nn}`)
setInterval(()=>{
	const myid = process.pid
	console.log(`[rov_main_entry  pid=${myid}]输出日志`,new Date())
},10000)
